package controllers

import (
	"bytes"
	"comment/filters"
	"comment/models"
	"comment/util"
	"encoding/json"
	"strconv"
	"strings"
	"time"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/validation"
)

type ReportController struct {
	BaseController
}

func (c *ReportController) Save() {
	var report models.Report
	result := make(map[string]interface{})
	var num string
	rn, err := util.Get("report_num")
	if err != nil {
		result["Code"] = 0
		result["Message"] = "无法生成举报编号后四位"
		beego.Error(err)
	} else {
		if rn == nil {
			util.Set("report_num", 1)
			util.Do("EXPIRE", "report_num", 60) //1分钟时间限制
		} else {
			util.Do("INCRBY", "report_num", 1)
		}
		count, err := util.GetInt("report_num")
		if err != nil {
			result["Code"] = 0
			result["Message"] = "无法生成举报编号后四位"
			beego.Error(err)
		} else {
			num = fomatNumber(count)
			if num == "ERROR" {
				result["Code"] = 0
				result["Message"] = "评论举报编号生成错误"
			} else {
				report.Report_num = num

				data := c.GetString("data")
				if data == "" {
					result["Code"] = 0
					result["Message"] = "入参为空"
					beego.Error("Report Save Error:入参为空")
				} else {
					if err := json.Unmarshal([]byte(data), &report); err == nil {
						b, r := Valida(&report)
						if !b {
							result["Code"] = r["Code"]
							result["Message"] = r["Message"]
						} else {
							timeNow := time.Now()
							report.Report_time = timeNow.Format("2006-01-02 03:04:05")
							id, err := models.SaveReport(&report)
							if err != nil {
								result["Code"] = 0
								result["Message"] = err
								beego.Error(err)
							} else {
								result["Code"] = 1
								result["Message"] = id
							}
						}

					} else {
						result["Code"] = 0
						result["Message"] = "入参格式不正确"
						beego.Error("Report Save Error:入参为空")
					}
				}
			}
		}
	}
	c.Data["json"] = result
	c.ServeJSON()
}

func (c *ReportController) Details() {
	c.Data["PageTitle"] = "举报详情"
	str := c.Ctx.Input.Param(":id")
	var report models.Report
	if strings.HasPrefix(str, "PLJB") {
		report = models.FindReportByNum(str)
	} else {
		id, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
		report = models.FindReportById(id)
	}
	c.Data["Report"] = report
	if report.Id != 0 {
		tcount, err := models.FindCountByUserIdAndStatus(report.User_id, 1)
		if err == nil {
			c.Data["TCount"] = tcount
		}
		jcount, err := models.FindCountByUserIdAndStatus(report.User_id, 2)
		if err == nil {
			c.Data["JCount"] = jcount
		}
		count, err := models.FindCountByUserIdAndStatus(report.User_id, 9)
		if err == nil {
			c.Data["Count"] = count
		}

		btcount, err := models.FindBeCountByUserIdAndStatus(report.Be_user_id, 1)
		if err == nil {
			c.Data["BTCount"] = btcount
		}
		bjcount, err := models.FindBeCountByUserIdAndStatus(report.Be_user_id, 2)
		if err == nil {
			c.Data["BJCount"] = bjcount
		}
		bcount, err := models.FindBeCountByUserIdAndStatus(report.Be_user_id, 9)
		if err == nil {
			c.Data["BCount"] = bcount
		}
		c.Layout = "layout/layout.html"
		c.TplName = "report/details_dsh.html"
	} else {
		c.Ctx.WriteString("未找到举报记录详情")
	}

}
func (c *ReportController) Audit() {
	resultMap := make(map[string]interface{})
	id, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
	var report models.Report
	report.Id = id
	status, _ := c.GetInt("Audit_status")
	report.Audit_status = status
	if status == 2 {
		jj, _ := c.GetInt("Refused_status")
		eyjb, _ := c.GetInt("is_malicious")
		report.Refused_status = jj
		report.Is_malicious = eyjb
	}
	opinion := c.GetString("Audit_opinion")
	report.Audit_opinion = opinion
	timeNow := time.Now()
	report.Audit_time = timeNow.Format("2006-01-02 03:04:05")
	report.Audit_user_id = 1
	report.Audit_user_name = filters.LoginInfo(c.Ctx).Name
	result, err := models.UpdateReport(&report)
	if err != nil {
		resultMap["Code"] = 0
		resultMap["Message"] = "审核失败"
	} else {
		resultMap["Code"] = 1
		resultMap["Message"] = result
	}
	c.Data["json"] = resultMap
	c.ServeJSON()
}

func (c *ReportController) List() {
	status, _ := strconv.Atoi(c.Ctx.Input.Param(":status"))
	c.Data["PageTitle"] = "评论举报列表"
	pageNo, _ := c.GetInt("PageNo")
	if pageNo == 0 {
		pageNo = 1
	}
	sizePage := c.GetString("SizePage")
	s, _ := strconv.Atoi(sizePage)
	if s == 0 {
		s = 10
	}
	var query = make(map[string]string)
	if v := c.GetString("Report_num"); v != "" {
		query["Report_num__icontains"] = v
		c.Data["Report_num"] = v
	}
	if v := c.GetString("Phone"); v != "" {
		query["Phone__icontains"] = v
		c.Data["Phone"] = v
	}
	if v := c.GetString("Be_report_phone"); v != "" {
		query["Be_report_phone__icontains"] = v
		c.Data["Be_report_phone"] = v
	}
	if v := c.GetString("Article_title"); v != "" {
		query["Article_title__icontains"] = v
		c.Data["Article_title"] = v
	}
	if v := c.GetString("Audit_user_name"); v != "" {
		query["Audit_user_name__icontains"] = v
		c.Data["Audit_user_name"] = v
	}

	if v := c.GetString("rbtime"); v != "" {
		query["Report_time__gte"] = v
		c.Data["rbtime"] = v
	}

	if v := c.GetString("retime"); v != "" {
		query["Report_time__lte"] = v
		c.Data["retime"] = v
	}

	if v := c.GetString("abtime"); v != "" {
		query["Audit_time__gte"] = v
		c.Data["abtime"] = v
	}

	if v := c.GetString("aetime"); v != "" {
		query["Audit_time__lte"] = v
		c.Data["aetime"] = v
	}

	c.Data["SizePage"] = s
	c.Data["Page"] = models.PageReport(query, status, pageNo, s)
	c.Layout = "layout/layout.html"
	if status == 0 {
		c.TplName = "report/report_dsh.html"
	} else if status == 1 {
		c.TplName = "report/report_ysh.html"
	} else {
		c.TplName = "report/report_shjj.html"
	}

}
func (c *ReportController) FindReport() {
	id, _ := c.GetInt("userId")
	result, err := models.FindRportByReportUserId(id)
	if err == nil {
		c.Data["json"] = result
	} else {
		beego.Error(err)
	}
	c.ServeJSON()
}

func Valida(ob interface{}) (bool, map[string]interface{}) {
	valid := validation.Validation{}
	b, err := valid.Valid(ob)
	result := make(map[string]interface{})
	if err != nil {
		beego.Error("validation Error")
		beego.Error(err)
		result["Code"] = 0
		result["Message"] = err
		return false, result
	}
	if !b {
		var myMap map[string]string
		myMap = make(map[string]string)

		for _, err := range valid.Errors {
			myMap[err.Key] = err.Message
			beego.Error("validation Info Error")
		}
		result["Code"] = 0
		result["Message"] = myMap
		return false, result
	} else {
		result["Code"] = 1
		return true, result
	}
}
func fomatNumber(count int) string {
	timeNow := time.Now()
	var dbstr bytes.Buffer
	dbstr.WriteString(timeNow.Format("PLJB200601020304"))
	var tempStr string
	if count != 0 && count <= 9999 {
		str1 := strconv.Itoa(count)
		if len(str1) == 1 {
			tempStr = "000" + str1
		} else if len(str1) == 2 {
			tempStr = "00" + str1
		} else if len(str1) == 3 {
			tempStr = "0" + str1
		} else {
			tempStr = str1
		}
		dbstr.WriteString(tempStr)
		return dbstr.String()
	} else {
		return "ERROR"
	}

}
